package indi.zhifa.recipe.bailan.framework.controller.api;

import indi.zhifa.recipe.bailan.framework.common.entity.annotations.UnLogin;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import indi.zhifa.recipe.bailan.framework.common.entity.dto.RestResponse;
import indi.zhifa.recipe.bailan.framework.mysql.entity.po.SysBaseEntity;
import indi.zhifa.recipe.bailan.framework.redis.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "test-redis测试")
@RequestMapping("/api/test/redis")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class RedisTestApi {

    private final RedisUtil mRedisUtil;
    private final String TEST_REDIS_HASH_KEY = "TEST_HASH:";
    private final String TEST_REDIS_SINGLE_KEY = "TEST_SINGLE:";

    @UnLogin
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "hmset-设置全部")
    @PostMapping("/hash/{key}")
    public String hmset(
            @Parameter(description = "redis-key") @PathVariable(name = "key") String pKey,
            @Parameter(description = "类型配置") @RequestBody Map<String,Object> pData){
        mRedisUtil.hmset(TEST_REDIS_HASH_KEY+pKey,pData);
        return "设置成功";
    }

    @UnLogin
    @Operation(summary = "hmget-获取hash值")
    @GetMapping("/hash/{key}")
    public Map hmget(@Parameter(description = "类型Id") @PathVariable(name = "key") String pKey){
        Map rtn = mRedisUtil.hmget(TEST_REDIS_HASH_KEY+pKey);
        return rtn;
    }

    @UnLogin
    @Operation(summary = "hget-获取单个值")
    @GetMapping ("/hash/{key}/{item}")
    public RestResponse<Object> hget(@Parameter(description = "类型Id") @PathVariable(name = "key") String pKey,
                      @Parameter(description = "类型Id") @PathVariable(name = "item") String pItem){
        Object data = mRedisUtil.hget(TEST_REDIS_HASH_KEY+pKey,pItem);
        return RestResponse.ok(data);
    }

    @UnLogin
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "hset-设置")
    @PutMapping ("/hash/{key}/{item}/object")
    public String hset(@Parameter(description = "键") @PathVariable(name = "key") String pKey,
                       @Parameter(description = "子键") @PathVariable(name = "item") String pItem,
                       @Parameter(description = "类型配置") @RequestBody Object pData){
        mRedisUtil.hset(TEST_REDIS_HASH_KEY+pKey,pItem,pData);
        return "设置成功";
    }


    @UnLogin
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "hash-删除键")
    @DeleteMapping ("/hash/{key}/{item}")
    public String hdel(@Parameter(description = "类型Id") @PathVariable(name = "key") String pKey,
                         @Parameter(description = "类型Id") @RequestParam(name = "item") String pItem){
        mRedisUtil.hdel(TEST_REDIS_HASH_KEY+pKey,pItem);
        return "删除成功";
    }

    @UnLogin
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "set-设置值")
    @PostMapping("/single/{key}")
    public String set(
            @Parameter(description = "redis-key") @PathVariable(name = "key") String pKey,
            @Parameter(description = "类型配置") @RequestBody Object pData){
        mRedisUtil.set(TEST_REDIS_SINGLE_KEY+pKey,pData);
        return "设置成功";
    }

    @UnLogin
    @Operation(summary = "get-获取值")
    @GetMapping("/single/{key}")
    public RestResponse<Object> get(@Parameter(description = "类型Id") @PathVariable(name = "key") String pKey){
        long beg = System.currentTimeMillis();
        Object rtn = mRedisUtil.get(TEST_REDIS_SINGLE_KEY+pKey);
        long end = System.currentTimeMillis();
        log.info("获取"+TEST_REDIS_SINGLE_KEY+pKey+"消耗"+(end-beg)+"ms");
        return RestResponse.ok(rtn);
    }

    @UnLogin
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "delete-删除键")
    @DeleteMapping ("/single/{key}/{item}")
    public String delete(@Parameter(description = "类型Id") @PathVariable(name = "key") String pKey){
        mRedisUtil.delete(TEST_REDIS_SINGLE_KEY+pKey);
        return "删除成功";
    }


    @UnLogin
    @Operation(summary = "increment-整数值增长")
    @GetMapping("/int/{key}/increase")
    public Integer increase(@Parameter(description = "类型Id") @PathVariable(name = "key") String pKey,
                            @Parameter(description = "增长值") @RequestParam(name = "delta") Integer pDelta){
        Long rtn = mRedisUtil.increment(TEST_REDIS_SINGLE_KEY+pKey,pDelta);
        return rtn.intValue();
    }

}
